package com.test.mybatis.generator;

import com.alibaba.fastjson.JSON;
import org.junit.Test;
import org.junit.platform.commons.util.StringUtils;

import java.io.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.*;

/**
 * @author HelSing
 * @date 2019/3/18
 */
public class RulesCommentGenerator {

    private static List<String> c = new ArrayList<>();

    private static String path = "/Users/mac/dome/";

    static {
        //哪个个表
        c.add("security");
    }

    @Test
    public void run() throws Exception {
        htmlCode();
    }

    private static void rulesCode() throws Exception {
        List<String> cz = new ArrayList<>();
        String url = "jdbc:mysql://rm-8vb43b03vawbw24k0oo.mysql.zhangbei.rds.aliyuncs.com:3306/yfgranary?serverTimezone=GMT";
        String user = "root";
        String password = "Jiajiale5";
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, user, password);
        DatabaseMetaData metaData = connection.getMetaData();

        //获取数据库下面所有表
        ResultSet tables = metaData.getTables("yfgranary", null, "%", new String[]{"TABLE"});

        while (tables.next()) {
            // 列的个数
            int columnCount = tables.getMetaData().getColumnCount();
            // 根据指定列名称获取数据
            String TABLE_NAME = tables.getString("TABLE_NAME");
            String TABLE_SCHEM = tables.getString("TABLE_SCHEM");
            if (cz.contains(TABLE_NAME)) {
                continue;
            }
            if (!c.isEmpty()) {
                if (!c.contains(TABLE_NAME)) {
                    continue;
                }
            }
            ResultSet columns = metaData.getColumns("yfgranary", "%", TABLE_NAME, "%");

            Map<String, List<RulesDetail>> property = new HashMap<>();
            while (columns.next()) {

                String COLUMN_NAME = columns.getString("COLUMN_NAME");
                String COLUMN_NAME_UP = convertName(COLUMN_NAME);
                String TYPE_NAME = columns.getString("TYPE_NAME");
                String REMARKS = columns.getString("REMARKS");
                String NULLABLE = columns.getString("NULLABLE");
                String COLUMN_SIZE = columns.getString("COLUMN_SIZE");
                String DECIMAL_DIGITS = columns.getString("DECIMAL_DIGITS");
                //System.out.println(COLUMN_NAME + " is ".concat(NULLABLE));
                if (COLUMN_NAME_UP.equals("created") || COLUMN_NAME_UP.equals("createUser") ||
                        COLUMN_NAME_UP.equals("createUserName") || COLUMN_NAME_UP.equals("modified") ||
                        COLUMN_NAME_UP.equals("modificationUserName") || COLUMN_NAME_UP.equals("modificationUserId")
                        || COLUMN_NAME_UP.equals("yn") || COLUMN_NAME_UP.equals("id")
                ) {
                    continue;
                }
                if (NULLABLE.equals("0")) {
                    List<RulesDetail> d = new ArrayList<>();
                    RulesDetail r = new RulesDetail();
                    d.add(r);
                    String rem[] = REMARKS.split(" ");
                    String text = "请输入";
                    if (rem.length > 1 && REMARKS.contains(":")) {
                        text = "请选择";
                    }
                    r.setRequired(true);
                    r.setMessage(text + rem[0]);
                    if(TYPE_NAME.equals("VARCHAR")) {
                        if (rem.length == 1 && !REMARKS.contains(":")) {
                            RulesDetail r1 = new RulesDetail();
                            r1.setMin(1);
                            r1.setMax(Integer.parseInt(COLUMN_SIZE));
                            r1.setMessage("不能超过 1 到 " + r1.getMax()+ " 个字");
                            d.add(r1);
                        }
                    }
                    if(TYPE_NAME.toLowerCase(Locale.ROOT).equals("decimal")) {
                        RulesDetail r2 = new RulesDetail();
                        r2.setPattern("/^[1-9]\\d{0,"+ COLUMN_SIZE +"}(\\.\\d{1,"+ DECIMAL_DIGITS +"})?$|^0(\\.\\d{1," + DECIMAL_DIGITS + "})?$/");
                        r2.setMessage("整数位不能超过" + COLUMN_SIZE + ",小数位不能超过" + DECIMAL_DIGITS);
                        d.add(r2);
                    }
                    if(TYPE_NAME.toLowerCase(Locale.ROOT).equals("int")) {
                        RulesDetail r2 = new RulesDetail();
                        r2.setPattern("/^[\\-\\+]?[1-9]\\d*$/");
                        r2.setMessage("请勿包含非法字符");
                        d.add(r2);
                    }
                    property.put(COLUMN_NAME_UP, d);
                }
            }
            System.out.println(JSON.toJSONString(property));
        }

    }

    private static void htmlCode() throws Exception {
        List<String> cz = new ArrayList<>();
        String url = "jdbc:mysql://rm-8vb43b03vawbw24k0oo.mysql.zhangbei.rds.aliyuncs.com:3306/yfgranary?serverTimezone=GMT";
        String user = "root";
        String password = "Jiajiale5";
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, user, password);
        DatabaseMetaData metaData = connection.getMetaData();

        //获取数据库下面所有表
        ResultSet tables = metaData.getTables("yfgranary", null, "%", new String[]{"TABLE"});

        while (tables.next()) {
            // 列的个数
            int columnCount = tables.getMetaData().getColumnCount();
            // 根据指定列名称获取数据
            String TABLE_NAME = tables.getString("TABLE_NAME");
            String TABLE_SCHEM = tables.getString("TABLE_SCHEM");
            if (cz.contains(TABLE_NAME)) {
                continue;
            }
            if (!c.isEmpty()) {
                if (!c.contains(TABLE_NAME)) {
                    continue;
                }
            }
            ResultSet columns = metaData.getColumns("yfgranary", "%", TABLE_NAME, "%");
            StringBuilder h = new StringBuilder();

            while (columns.next()) {

                String COLUMN_NAME = columns.getString("COLUMN_NAME");
                String COLUMN_NAME_UP = convertName(COLUMN_NAME);
                String TYPE_NAME = columns.getString("TYPE_NAME");
                String REMARKS = columns.getString("REMARKS");
                String NULLABLE = columns.getString("NULLABLE");
                String COLUMN_SIZE = columns.getString("COLUMN_SIZE");
                //System.out.println(COLUMN_NAME + " is ".concat(NULLABLE));
                if (COLUMN_NAME_UP.equals("created") || COLUMN_NAME_UP.equals("createUserId") ||
                        COLUMN_NAME_UP.equals("createUserName") || COLUMN_NAME_UP.equals("modified") ||
                        COLUMN_NAME_UP.equals("modificationUserId") || COLUMN_NAME_UP.equals("modificationUserName")
                        || COLUMN_NAME_UP.equals("yn") || COLUMN_NAME_UP.equals("id")
                ) {
                    continue;
                }
                h.append("<el-col :xs=\"24\" :sm=\"24\" :md=\"8\" :lg=\"7\" :xl=\"6\">\n");
                String str[] = REMARKS.split(" ");
                if (NULLABLE.equals("0")) {
                    h.append("<el-form-item label=\"" + str[0] +":\" prop=\"" + COLUMN_NAME_UP + "\">\n");
                } else {
                    h.append("<el-form-item label=\"" + str[0] + ":\">\n");
                }
                if (str.length > 1 && REMARKS.contains(":")) {
                    if (TYPE_NAME.equals("VARCHAR")) {
                        h.append("<el-select v-model=\"" + COLUMN_NAME_UP + "\" placeholder=\"请选择\">\n");
                        for (int i = 1, l = str.length; i < l; i++) {
                            String s = str[i];
                            if (s.trim().equals("") || s.trim().equals("默认")){
                                continue;
                            }
                            Map<String, String> m = new HashMap<>();
                            String[] d = s.split(":");
                            if(d.length > 1) {
                                h.append("<el-option label=\"" + d[1] + "\" value=\"" + d[0] + "\"></el-option>\n");
                            }
                        }
                        h.append("</el-select>\n");
                    } else {
                        h.append("<el-radio-group v-model=\"" + COLUMN_NAME_UP + "\" >\n");
                        for (int i = 1, l = str.length; i < l; i++) {
                            String s = str[i];
                            if (s.trim().equals("") || s.trim().equals("默认")){
                                continue;
                            }
                            Map<String, String> m = new HashMap<>();
                            String[] d = s.split(":");
                            if(d.length > 1) {
                                h.append("<el-radio-button label=\"" + d[0] + "\">" + d[1] + "</el-radio-button>\n");
                            }
                        }
                        h.append("</el-radio-group>\n");
                    }

                } else if ("DATETIME".equals(TYPE_NAME)) {
                    h.append("<el-date-picker type=\"date\" v-model=\""+COLUMN_NAME_UP+"\" clearable></el-date-picker>\n");
                } else {
                    h.append("<el-input v-model=\"" + COLUMN_NAME_UP + "\"></el-input>\n");
                }
                h.append("</el-form-item>\n</el-col>\n");
            }
            System.out.println(h.toString());
        }

    }

    private static String convertName(String columnName) {
        StringBuilder sb = new StringBuilder();
        String[] str = columnName.split("_");
        sb.append(str[0]);
        for (int i = 1, l = str.length; i < l; i++) {
            String s = str[i];
            sb.append(s.substring(0, 1).toUpperCase(Locale.ROOT));
            sb.append(s.substring(1));
        }
        return sb.toString();
    }
}


